package com.lianxi.controller.admin;


import com.lianxi.mapper.UserMapper;
import com.lianxi.pojo.Role;
import com.lianxi.pojo.User;
import com.lianxi.service.UserService;
import com.lianxi.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Api(tags = "用户管理接口")
public class UserManageController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/userslist")
    @ApiOperation("获取用户列表")
    public CommonResult getAllUsers(@RequestParam(value = "nickname",defaultValue = "") String nickname){
        List<User> allUsers = userService.getAllUsers(nickname);
        if (allUsers!=null && allUsers.size()>0){
            return CommonResult.success(allUsers);
        }
        return CommonResult.fail();
    };

    @DeleteMapping("/deleuser/{uid}")
    @ApiOperation("根据uId删除某用户")
    public CommonResult deleteUserById(@PathVariable("uid") Integer uid){
        boolean result = userService.deleteUser(uid);
        return result?CommonResult.success(result):CommonResult.failHaveData(result);
    }

    @PutMapping("/upenabled/{uid}")
    @ApiOperation("更新用户状态，可用与不可用")
    public CommonResult updateEnable(@PathVariable("uid") Integer id){
        int result = userService.updateEnable(id);
        if (result<=0){
            return CommonResult.fail();
        }else {
            return CommonResult.success(result);
        }
    }

    @GetMapping("/getallroles")
    @ApiOperation("获取角色列表")
    public CommonResult getAllRoles(){
        List<Role> allRoles = userService.getAllRoles();
        if (allRoles!=null && allRoles.size()>0){
            return CommonResult.success(allRoles);
        }
        return CommonResult.fail();
    };

    @PostMapping("/addroles")
    @ApiOperation("更新用户权限")
    public CommonResult doUserToRoles(Integer id,int[] roles){

        int result = userService.doUserToRoles(id, roles);
        if(result<=0){
            return CommonResult.failHaveData(result);
        }else {
            return CommonResult.success(result);
        }
    }

}
